package com.epro.standard;

import java.io.PrintStream;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import com.epro.standard.frontend.util.StringUtil;


/**
 * @author Juatrat
 * updated Date 13-05-2003
 */
public class StandardException extends Exception {
    public static final Locale THAI = new java.util.Locale("th", "TH");
    public static final Locale ENGLISH = new java.util.Locale("en", "US");
    private static final String BUNDLE_NAME = 
        "com.epro.standard.StandardException"; //$NON-NLS-1$
    private static final ResourceBundle RESOURCE_BUNDLE = 
        ResourceBundle.getBundle(BUNDLE_NAME);
    private ResourceBundle resourceBundle = null;
    private Exception e = null;
    private String id = null;
    private ArrayList message = null;
    private String errorMessage = null;

    /**
     * @param java.lang.String id
     */
    public StandardException(String id) {
        this.id = id;
    }

    public String getMessageFromID() {
        return (this.id != null) ? RESOURCE_BUNDLE.getString(id) : null;
    }

    public String getMessageFromIDWithArg() {
        String str = null;
        StringBuffer sb = new StringBuffer();
        if (this.id != null) {
            str = RESOURCE_BUNDLE.getString(id);
            if (message != null)
                for (int i = 0; i < message.size(); i++) {
                    sb.append(StringUtil.replace(str, "%" + i, 
                                                 (String)message.get(i), 
                                                 false));
                    //sb.append("\n");
                }
        }
        return sb.toString();
    }

    public StandardException(String id, Exception e) {
        this.e = e;
        this.id = id;
    }

    /**
     * @param java.lang.String id
     * @param java.lang.String message1
     */
    public StandardException(String id, String message1) {
        this(id);
        message = new ArrayList();
        message.add(message1);
    }

    /**
     * @param java.lang.String id
     * @param java.lang.String message1
     * @param java.lang.String message2
     */
    public StandardException(String id, String message1, String message2) {
        this(id, message1);
        message.add(message2);
    }

    /**
     * @param java.lang.String id
     * @param java.lang.String message1
     * @param java.lang.String message2
     * @param java.lang.String message3
     */
    public StandardException(String id, String message1, String message2, 
                             String message3) {
        this(id, message1, message2);
        message.add(message3);
    }

    /**
     * @param Exception e
     */
    public StandardException(Exception e) {
        this.e = e;
    }

    public void printStackTrace() {
        System.out.println("-----------  Standard Exception : printStackTrace ------------");

        if (e != null) {
            e.printStackTrace();
        }

        if (id != null) {
            System.out.println("Error Code = " + id);
            System.out.println(RESOURCE_BUNDLE.getString(id));
        }

        if (errorMessage != null) {
            System.out.println("Error Message = " + errorMessage);
        }

        if (message != null) {
            for (int i = 0; i < message.size(); i++)
                System.out.println("Message = " + message.get(i));
        }
    }

    /**
     * @param java.io.PrintStream out
     */
    public void printStackTrace(PrintStream out) {
        if (e != null) {
            e.printStackTrace(out);
        }
    }

    /**
     * @param java.io.PrintWriter out
     */
    public void printStackTrace(PrintWriter out) {
        if (e != null) {
            e.printStackTrace(out);
        }
    }

    /**
     * @return java.lang.String
     */
    public String getDescription() {
        return getDescription(null);
    }

    /**
     * @param java.util.Locale locale
     * @return java.lang.String
     */
    public String getDescription(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }

        if (id != null) {
            resourceBundle = 
                    java.util.ResourceBundle.getBundle(this.getClass().getName(), 
                                                       locale);

            String keyword = resourceBundle.getString("KEYWORD");
            String description = "";

            try {
                StringTokenizer stk = 
                    new StringTokenizer(resourceBundle.getString(id), "|");
                description = stk.nextToken();
            } catch (MissingResourceException mre) {
                description = "This Error is not defined in properties file";
            }

            if (message != null) {
                StringBuffer desc = new StringBuffer(description);
                int index = desc.toString().indexOf(keyword);

                for (int i = 0; (i < message.size()) && (index != -1); i++) {
                    desc = 
desc.replace(index, index + keyword.length(), (String)message.get(i));
                    index = desc.toString().indexOf(keyword);
                }

                description = desc.toString();
            }

            return description;
        } else if (e != null) {
            return e.getMessage();
        } else {
            return "";
        }
    }

    /**
     * @return java.lang.String
     */
    public String getSolution() {
        return getSolution(null);
    }

    /**
     * @param java.util.Locale locale
     * @return java.lang.String
     */
    public String getSolution(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }

        if (id != null) {
            resourceBundle = 
                    java.util.ResourceBundle.getBundle(this.getClass().getName(), 
                                                       locale);

            StringTokenizer stk = 
                new StringTokenizer(resourceBundle.getString(id), "|");
            stk.nextToken();

            if (stk.hasMoreTokens()) {
                return stk.nextToken();
            }
        }

        return "";
    }
}
